package com.bsoft.hospitalchs.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.DatePicker;

import com.bsoft.hospitalchs.R;

import java.util.Calendar;

/**
 * Created by 泅渡者
 * Created on 2017/6/16.
 */
public class CustomDialog extends Dialog {
    int layoutRes;//布局文件
    public DatePicker tm_picker;
    Context context;
    private Calendar calendar = null;
    private int year;
    private int month;
    private int day;

    public CustomDialog(Context context) {
        super(context);
        this.context = context;
    }
    /**
     * 自定义布局的构造方法
     * @param context
     * @param resLayout
     */
    public CustomDialog(Context context, int resLayout){
        super(context);
        this.context = context;
        this.layoutRes=resLayout;
    }
    /**
     * 自定义主题及布局的构造方法
     * @param context
     * @param theme
     * @param resLayout
     */
    public CustomDialog(Context context, int theme,int resLayout){
        super(context, theme);
        this.context = context;
        this.layoutRes=resLayout;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(layoutRes);
        tm_picker = (DatePicker) this.findViewById(R.id.tm_picker);

        // 获取日历对象。
        calendar = Calendar.getInstance();
        // 获取年月日时分秒的信息
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH)+1;      // 注意点，一月是从0开始计算的！！！
        day = calendar.get(Calendar.DAY_OF_MONTH);


        tm_picker.init(year, calendar.get(Calendar.MONTH), day, new DatePicker.OnDateChangedListener() {

            @Override
            // 参数依次是：view，year,monthOfYear,dayOfMonth
            public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3) {
                year=arg1;
                month = arg2+1;
                day = arg3;
            }
        });
    }
    public String getCurrentDate(){
        return year+"-"+format(month)+"-"+format(day);
    }

    public String  format(int str){
        if(str<=9){
            return "0"+str;
        }
        return str+"";
    }

}
